/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import dao.LiveServersDao;
import dao.LiveServersTaskDao;
import dao.MainScheduleTaskDao;
import dao.ProviderDao;
import dao.ScheduleDataDao;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import pojo.CMST;
import pojo.LiveServers;
import pojo.MyLST;
import pojo.Provider;
import pojo.ProviderTask;
import pojo.Scheduledata;

/**
 *
 * @author A7med Kotb
 */
//class that handles all schedule tasks on windows
public class ScheduledTaskWindows {

    public static void main(String[] args) {
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
        String sdate = df.format(new Date());
        System.out.println("dffffff=" + ((SimpleDateFormat) df).toPattern().replaceAll(sdate, sdate));
    }
    private Connection c;
    private Element pathelement;
    private Element tempelement;
    private Element maintaskjar;
    private Element liveserverjar;

    public ScheduledTaskWindows(Connection c) {
        try {
            this.c = c;
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document config = builder.parse("C:/Users/A7med Kotb/Documents/NetBeansProjects/trunk/Book Dimension Project/src/java/Configuration/winconfig.xml");
            pathelement = (Element) config.getElementsByTagName("jar-path").item(0);
            maintaskjar = (Element) config.getElementsByTagName("main-task-jar").item(0);
            tempelement = (Element) config.getElementsByTagName("temporery-path").item(0);
            liveserverjar = (Element) config.getElementsByTagName("live-server-jar").item(0);
            System.out.println("Jar File Path is " + pathelement);
            System.out.println("Main Task Jar File Path is : " + maintaskjar);
            System.out.println("Temporary Path is " + tempelement);
        } catch (SAXException ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the following Exception while trying to parse the XML file " + ex.getMessage());
        } catch (IOException ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the following Exception while trying to parse the XML file " + ex.getMessage());
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the following Exception while trying to parse the XML file " + ex.getMessage());
        }
    }

    //this method for creating new schedule task on Windows...
    public boolean create(Scheduledata data, String providername) {
        boolean check = true;
        try {
            LogFile.log(" System ", " started in creating Schedule task for Provider [" + providername + "] on Windows Operating System.");
            String username = System.getProperty("user.name");
            String taskname = providername + "ST";
            String modifier = "";
            String day = "";
            String month = "";
            String pattern = "MM/dd/yyyy";
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            String sdate = df.format(new Date());
            if (data.getSchedule().equalsIgnoreCase("hourly")) {
                data.setDay(null);
                if (data.getModifier() == null && data.getModifier().equals("")) {
                    modifier = " /mo 1 ";
                }
            }
            if (data.getSchedule().equalsIgnoreCase("daily")) {
                data.setDay(null);
                if (data.getModifier() == null && data.getModifier().equals("")) {
                    modifier = " /mo 1 ";
                }
            }
            if (data.getSchedule().equalsIgnoreCase("weekly")) {
                if (data.getModifier() == null && data.getModifier().equals("")) {
                    modifier = " /mo 1 ";
                }
            }
            if (data.getModifier() != null && !data.getModifier().equals("")) {
                modifier = " /mo " + data.getModifier() + "  ";
            }
            if (data.getDay() != null && !data.getDay().equals("")) {
                day = "  /d  " + data.getDay() + " ";
            }
            if (data.getMonth() != null && !data.getMonth().equals("")) {
                month = " /m " + data.getMonth() + " ";
            }

            String tasktxt = "Schtasks /create /ru \"" + username + "\" /SC " + data.getSchedule() + modifier + "  /TN " + taskname + " /TR  \"javaw -jar  " + pathelement.getTextContent() + "  " + providername + " " + "\"  /sd " + sdate + day + month + " /ST " + data.getStartTime();
            String path = tempelement.getTextContent() + providername + ".bat";
            System.out.println("path A7med Kotb =" + path);
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            DeleteThread dt = new DeleteThread(path);
            LogFile.log("System", " Successffully created Schedule Task for provider " + providername + " and that schedule task will run every " + data.getModifier() + " " + data.getSchedule() + " at " + data.getStartTime() + " starting from " + data.getStartDate());
        } catch (IOException ex) {
            check = false;
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log("System", "failed to create the schedule task for provider [" + providername + "] and the reason is the following exception [" + ex + "]");
        }
        return check;
    }
    //method to get all provider Names and tasks

    public Vector<ProviderTask> getAllProviderTasks() {
        Vector<ProviderTask> prtsvect = new Vector<ProviderTask>();
        try {
            ScheduleDataDao scdao = new ScheduleDataDao(c);
            Vector<Scheduledata> scdata = scdao.getall();
            ProviderDao prdao = new ProviderDao(c);
            Vector<Provider> prdata = prdao.getAll();
            for (int i = 0; i < prdata.size(); i++) {
                ProviderTask prts = new ProviderTask();
                prts.setpId(prdata.elementAt(i).getPId());
                prts.setName(prdata.elementAt(i).getName());
                prts.setScId(scdata.elementAt(i).getScId());
                prts.setSchedule(scdata.elementAt(i).getSchedule());
                prts.setModifier(scdata.elementAt(i).getModifier());
                prts.setStartDate(scdata.elementAt(i).getStartDate());
                prts.setStartTime(scdata.elementAt(i).getStartTime());
                prts.setDay(scdata.elementAt(i).getDay());
                prts.setIsenable(scdata.elementAt(i).getIsenable());
                prts.setIsrunning(scdata.elementAt(i).getIsrunning());
                prts.setMonth(scdata.elementAt(i).getMonth());
                prtsvect.add(prts);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return prtsvect;
    }

    //method to get all available schedule tasks 
    public Vector<Scheduledata> getAllTasks() {
        try {
            ScheduleDataDao dao = new ScheduleDataDao(c);
            return dao.getall();
        } catch (Exception ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the following Exception while trying to display all available schedule tasks " + ex.getMessage());
        }
        return null;
    }

//this method changes the schedule task status to be enabled
    public boolean enable(int taskid) {
        boolean check = false;
        try {
            ScheduleDataDao dao = new ScheduleDataDao(c);
            Scheduledata sdata = dao.getask(taskid);
            sdata.setIsenable(true);
            boolean enableresult = dao.update(sdata);
            if (enableresult == true) {
                check = true;
            } else {
                check = false;
            }
            ProviderDao pdao = new ProviderDao(c);
            Provider provider = pdao.getById(taskid);
            String path = tempelement.getTextContent() + provider.getName() + "enable.bat";
            String tasktxt = "schtasks /change /tn " + provider.getName() + "ST  /enable";
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            DeleteThread dt = new DeleteThread(path);
        } catch (IOException ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the Following exception during enabling the the schedule task " + ex.getMessage());
        } catch (Exception ex) {
            LogFile.log(" System ", " encountered the Following exception during enabling the the schedule task " + ex.getMessage());
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
        return check;
    }

    //this method for disabling schedule task
    public boolean disable(int taskid) {
        boolean check = false;
        Provider provider = null;
        try {
            ScheduleDataDao dao = new ScheduleDataDao(c);
            Scheduledata sdata = dao.getask(taskid);
            sdata.setIsenable(false);
            sdata.setIsrunning(false);
            check = dao.update(sdata);

            ProviderDao pdao = new ProviderDao(c);
            provider = pdao.getById(taskid);

            String path = tempelement.getTextContent() + provider.getName() + "disable.bat";
            String tasktxt = "schtasks /change /tn " + provider.getName() + "ST  /disable";
            System.out.println(tasktxt);

            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();

            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);

            DeleteThread dt = new DeleteThread(path);
        } catch (IOException ex) {
            check = false;
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered this exception while trying to disable the task related to " + provider.getName() + " and this is the exception message " + ex.getMessage());
        } catch (Exception ex) {
            check = false;
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered this exception while trying to disable the task related to " + provider.getName() + " and this is the exception message " + ex.getMessage());
        }
        return check;
    }

    //method to stop running schedule task 
    public boolean stop(int taskid) {
        Provider provider = null;
        boolean check = false;
        try {
            ScheduleDataDao dao = new ScheduleDataDao(c);
            Scheduledata sdata = dao.getask(taskid);
            if (sdata.getIsrunning() == false) {
                System.out.println("Task Already Stopped");
                LogFile.log(" System ", " tried to stop already stopped schedule task");
            } else {
                sdata.setIsrunning(false);
                check = dao.update(sdata);
                ProviderDao pdao = new ProviderDao(c);
                provider = pdao.getById(taskid);

                String path = tempelement.getTextContent() + provider.getName() + "end.bat";
                String tasktxt = "schtasks /end /tn " + provider.getName() + "ST ";
                System.out.println(tasktxt);

                File batchfile = new File(path);
                PrintWriter fos = new PrintWriter(batchfile);
                fos.println(tasktxt);
                fos.println("exit");
                fos.close();

                Runtime run = Runtime.getRuntime();
                run.exec("cmd /c start " + path);
                System.out.println(tasktxt);
                DeleteThread dt = new DeleteThread(path);
            }
        } catch (IOException ex) {
            check = false;
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", "encountered the following exception while trying to stop schedule task reslated to provider " + provider.getName() + " and this is the error " + ex.getMessage());
        } catch (Exception ex) {
            check = false;
            LogFile.log(" System ", "encountered the following exception while trying to stop schedule task reslated to provider " + provider.getName() + " and this is the error " + ex.getMessage());
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
        return check;
    }

    // this method starts the specified schedule task now and changes status of it to be running
    public boolean start(int taskid) {
        boolean check = false;
        try {
            ProviderDao pdao = new ProviderDao(c);
            Provider provider = pdao.getById(taskid);

            ScheduleDataDao scdao = new ScheduleDataDao(c);
            Scheduledata scdata = scdao.getask(taskid);

            String path = tempelement.getTextContent() + provider.getName() + "start.bat";
            String tasktxt = "schtasks /run /tn " + provider.getName() + "ST ";
            System.out.println(tasktxt);
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            if (scdata.getIsenable()) {
                run.exec("cmd /c start " + path);
                scdata.setIsrunning(true);
                boolean updateresult = scdao.update(scdata);
                if (updateresult == true) {
                    check = true;
                } else {
                    check = false;
                }
            } else {
                check = false;
                System.out.println("Schedule Task is Disabled Please Enable it First.");
            }
            System.out.println(tasktxt);
            System.out.println("path is///////////" + path);
            DeleteThread dt = new DeleteThread(path);
        } catch (Exception ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the following exception while trying to start the schedule task " + ex.getMessage());
        }
        return check;
    }

    //method to resume the schedule task
    public boolean resume(int taskid) {
        boolean check = false;
        check = enable(taskid);
        check = start(taskid);
        return check;
    }

    //this method for deleting schedule task ...
    public boolean delete(int taskid) {
        boolean check = false;
        try {
            ScheduleDataDao dao = new ScheduleDataDao(c);
//            boolean deleteresult=dao.delete(taskid);
//            if(deleteresult == true)
//            {
//                check=true;
//            }
//            else
//            {
//                check=false;
//            }
            ProviderDao pdao = new ProviderDao(c);
            Provider provider = pdao.getById(taskid);
            String path = tempelement.getTextContent() + provider.getName() + "delete.bat";
            String tasktxt = "schtasks /delete /f /tn " + provider.getName() + "ST ";
            System.out.println(tasktxt);
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            DeleteThread dt = new DeleteThread(path);
        } catch (Exception ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the following exception while trying to delete the schedule task " + ex.getMessage());
        }
        return check;
    }

    //this method to end running schedule task
    public boolean end(int taskid) {
        boolean check = false;
        Provider provider = null;
        try {
            ScheduleDataDao dao = new ScheduleDataDao(c);
            Scheduledata scdata = dao.getask(taskid);
            scdata.setIsrunning(false);
            check = dao.update(scdata);

            ProviderDao pdao = new ProviderDao(c);
            provider = pdao.getById(taskid);

            String path = tempelement.getTextContent() + provider.getName() + "end.bat";
            String tasktxt = "schtasks /end /tn " + provider.getName() + "ST ";
            System.out.println(tasktxt);

            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();

            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            DeleteThread dt = new DeleteThread(path);
        } catch (Exception ex) {
            check = false;
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the following exception while trying to end the schedule task of the provider " + provider.getName() + " and this is the exception error " + ex.getMessage());
        }
        return check;
    }

    //method to create main schedule task 
    public boolean createmaintask(CMST cmst, String type) {
        boolean check = true;
        try {
            LogFile.log(" System ", " started in creating Main Schedule task With Name [" + cmst.getTaskName() + "] on Windows Operating System, and its type is :["+type+"]");
            String username = System.getProperty("user.name");
            String taskname = cmst.getTaskName() + "ST";

            String modifier = "";
            String day = "";
            String month = "";
            String pattern = "MM/dd/yyyy";
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            String sdate = df.format(new Date());
            if (cmst.getSchedule().equalsIgnoreCase("hourly")) {
                cmst.setDay(null);
                if (cmst.getModifier() == null && cmst.getModifier().equals("")) {
                    modifier = " /mo 1 ";
                }
            }
            if (cmst.getSchedule().equalsIgnoreCase("daily")) {
                cmst.setDay(null);
                if (cmst.getModifier() == null && cmst.getModifier().equals("")) {
                    modifier = " /mo 1 ";
                }
            }
            if (cmst.getSchedule().equalsIgnoreCase("weekly")) {
                if (cmst.getModifier() == null && cmst.getModifier().equals("")) {
                    modifier = " /mo 1 ";
                }
            }
            if (cmst.getModifier() != null && !cmst.getModifier().equals("")) {
                modifier = " /mo " + cmst.getModifier() + "  ";
            }
            if (cmst.getDay() != null && !cmst.getDay().equals("")) {
                day = "  /d  " + cmst.getDay() + " ";
            }
            if (cmst.getMonth() != null && !cmst.getMonth().equals("")) {
                month = " /m " + cmst.getMonth() + " ";
            }
            String tasktxt = "Schtasks /create /ru \"" + username + "\" /SC " + cmst.getSchedule() + modifier + "  /TN " + taskname + " /TR  \"javaw -jar  " + maintaskjar.getTextContent() + "  " + type + " " + "\"  /sd " + sdate + day + month + " /ST " + cmst.getStartTime();
            String path = tempelement.getTextContent() + taskname + ".bat";
            System.out.println("path A7med Kotb =" + path);
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();

            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            DeleteThread dt = new DeleteThread(path);
            LogFile.log("System", " Successffully created Main Schedule Task With Name " + cmst.getTaskName() + " and that schedule task will run " + cmst.getSchedule() + " at " + cmst.getStartTime() + " starting from date" + cmst.getStartDate());
        } catch (IOException ex) {
            check = false;
            LogFile.log(" System ", " encoutered the following Exception while trying to create main schedule task " + ex);
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
        return check;
    }

    //method to create liveserver task 
    public boolean createLiveServerTask(MyLST lst, LiveServers ls) {
        String LSObject = ls.getType() + "-" + ls.getIp() + "-" + ls.getPort() + "-" + ls.getUserName() + "-" + ls.getPassword();
        boolean check = true;
        try {
            LogFile.log(" System ", " started in creating Schedule Task For Live Srever With IP:[" + ls.getIp() + "] on Windows Operating System, and its type is :["+ls.getType()+"]");
            String username = System.getProperty("user.name");
            String taskname = ls.getIp() + "ST";

            String modifier = "";
            String day = "";
            String month = "";
            String pattern = "MM/dd/yyyy";
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            String sdate = df.format(new Date());
            if (lst.getSchedule().equalsIgnoreCase("hourly")) {
                lst.setDay(null);
                if (lst.getModifier() == null && lst.getModifier().equals("")) {
                    modifier = " /mo 1 ";
                }
            }
            if (lst.getSchedule().equalsIgnoreCase("daily")) {
                lst.setDay(null);
                if (lst.getModifier() == null && lst.getModifier().equals("")) {
                    modifier = " /mo 1 ";
                }
            }
            if (lst.getSchedule().equalsIgnoreCase("weekly")) {
                if (lst.getModifier() == null && lst.getModifier().equals("")) {
                    modifier = " /mo 1 ";
                }
            }
            if (lst.getModifier() != null && !lst.getModifier().equals("")) {
                modifier = " /mo " + lst.getModifier() + "  ";
            }
            if (lst.getDay() != null && !lst.getDay().equals("")) {
                day = "  /d  " + lst.getDay() + " ";
            }
            if (lst.getMonth() != null && !lst.getMonth().equals("")) {
                month = " /m " + lst.getMonth() + " ";
            }
            String tasktxt = "Schtasks /create /ru \"" + username + "\" /SC " + lst.getSchedule() + modifier + "  /TN " + taskname + " /TR  \"javaw -jar  " + liveserverjar.getTextContent() + "  " + LSObject + " " + "\"  /sd " + sdate + day + month + " /ST " + lst.getStartTime();

            String path = tempelement.getTextContent() + ls.getIp() + ".bat";
            System.out.println("path A7med Kotb =" + path);
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();

            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            DeleteThread dt = new DeleteThread(path);
            LogFile.log("System", " Successffully created Schedule Task for Live Server With IP:[ " + ls.getIp() + "], and that schedule task will run " + lst.getSchedule() + " at " + lst.getStartTime() + " starting from date" + lst.getStartDate());
        } catch (IOException ex) {
            check = false;
            LogFile.log(" System ", " encountered the following Exception while trying to Create schedule task for Live Server with IP :[ " + ls.getIp() + "], and  the Error was :[ " + ex+"]");
        }
        return check;
    }

    //method to delete live server task ...
    public boolean deleteLiveServerTask(Integer lstId) {
        LiveServers ls = null;
        boolean check = true;
        try {
            LiveServersDao lsdao = new LiveServersDao(c);
            ls = lsdao.getById(lstId);

            String path = tempelement.getTextContent() + ls.getIp() + "delete.bat";
            String tasktxt = "schtasks /delete /f /tn " + ls.getIp() + "ST ";
            System.out.println(tasktxt);

            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);

            DeleteThread dt = new DeleteThread(path);
        } catch (Exception ex) {
            check = false;
            LogFile.log(" System ", " encountered the following Exception while trying delete task for live server " + ls.getIp() + " and this is the error " + ex.getMessage());
            ex.printStackTrace();
        }
        return check;
    }

    //method to delete main task data 
    public boolean deleteMainTask(String taskName) {
        boolean check = true;
        try {
            String path = tempelement.getTextContent() + taskName + "delete.bat";
            String tasktxt = "schtasks /delete /f /tn " + taskName + "ST ";
            System.out.println(tasktxt);

            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            DeleteThread dt = new DeleteThread(path);
        } catch (Exception ex) {
            check = false;
            LogFile.log(" System ", " encountered the following Exception while trying to delete main task from system " + ex.getMessage());
            ex.printStackTrace();
        }
        return check;
    }

    //this method called when update in schedule task takeplace
    public boolean deleteAfterUpdate(int taskid) {
        boolean check = true;
        Provider provider=null;
        try {
            
            ProviderDao pdao = new ProviderDao(c);
            provider = pdao.getById(taskid);
            LogFile.log(" System  ", "now trying to delete the Old Schedule Task of the Provider ["+provider.getName()+",After the Update Operation.]");
            String path = "D:\\" + provider.getName() + "delete.bat";
            String tasktxt = "schtasks /delete /f /tn " + provider.getName() + "ST ";
            System.out.println(tasktxt);
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            LogFile.log(" System", " Successfully Deleted the Old Operation of the Provider ["+provider.getName()+",After the Update Operation .]");
            DeleteThread dt = new DeleteThread(path);
        } catch (Exception ex) {
            check = false;
            LogFile.log(" System ", " encountered the following exception while trying to delete the old Task of the Provider ["+provider.getName()+"] After the update process ,the Exception Message  [" +ex+"]");
        }
        return check;
    }

    public Vector<Provider> getAllProviders() {
        try {
            ProviderDao dao = new ProviderDao(c);
            return dao.getAll();
        } catch (Exception ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the following Exception while trying to display all available schedule tasks " + ex.getMessage());
        }
        return null;
    }

    //Method to enable Main Task 
    public boolean enableMainTask(String taskName) {
        boolean check = false;
        try {
            MainScheduleTaskDao dao = new MainScheduleTaskDao(c);
            CMST cmst = dao.getask(taskName);
            cmst.setIsEnable(true);
            boolean enableresult = dao.update(cmst);
            if (enableresult == true) {
                check = true;
            } else {
                check = false;
            }
            String path = tempelement.getTextContent() + cmst.getTaskName() + "enable.bat";
            String tasktxt = "schtasks /change /tn " + cmst.getTaskName() + "ST  /enable";
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            DeleteThread dt = new DeleteThread(path);
        } catch (IOException ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the Following exception during enabling the the schedule task " + ex.getMessage());
        } catch (Exception ex) {
            LogFile.log(" System ", " encountered the Following exception during enabling the the schedule task " + ex.getMessage());
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
        return check;
    }

    //method to disable main task
    public boolean disableMainTask(String taskName) {
        boolean check = false;
        try {
            MainScheduleTaskDao dao = new MainScheduleTaskDao(c);
            CMST cmst = dao.getask(taskName);
            cmst.setIsEnable(false);
            cmst.setIsRunning(false);
            boolean disableresult = dao.update(cmst);
            if (disableresult == true) {
                check = true;
            } else {
                check = false;
            }
            String path = tempelement.getTextContent() + cmst.getTaskName() + "disable.bat";
            String tasktxt = "schtasks /change /tn " + cmst.getTaskName() + "ST  /disable";
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            DeleteThread dt = new DeleteThread(path);
        } catch (IOException ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the Following exception during disabling the the schedule task " + ex.getMessage());
        } catch (Exception ex) {
            LogFile.log(" System ", " encountered the Following exception during disabling the the schedule task " + ex.getMessage());
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
        return check;
    }

    //method to start main task now
    public boolean startMainTask(String taskName) {
        boolean check = false;
        MainScheduleTaskDao mstdao = new MainScheduleTaskDao(c);
        CMST cmst = mstdao.getask(taskName);
        try {
            String path = tempelement.getTextContent() + taskName + "start.bat";
            String tasktxt = "schtasks /run /tn " + taskName + "ST ";
            System.out.println(tasktxt);
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            if (cmst.getIsEnable()) {
                run.exec("cmd /c start " + path);
                cmst.setIsRunning(true);
                boolean updateresult = mstdao.update(cmst);
                if (updateresult == true) {
                    check = true;
                } else {
                    check = false;
                }
            } else {
                check = false;
                System.out.println("Schedule Task is Disabled Please Enable it First.");
            }
            System.out.println(tasktxt);
            System.out.println("path is///////////" + path);
            DeleteThread dt = new DeleteThread(path);
        } catch (Exception ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the following exception while trying to start the schedule task " + ex.getMessage());
        }
        return check;
    }

    //method to start Live Server Task
    public boolean startLiveServerTask(String serverIP) {
        boolean check = false;
        LiveServersDao lsd = new LiveServersDao(c);
        LiveServers ls = lsd.getByIp(serverIP);
        LiveServersTaskDao lstd = new LiveServersTaskDao(c);
        MyLST lst = null;
        try {
            String path = tempelement.getTextContent() + serverIP + "start.bat";
            String tasktxt = "schtasks /run /tn " + serverIP + "ST ";
            System.out.println(tasktxt);
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            lst = lstd.getById(ls.getLsId());
            if (lst.getIsEnable()) {
                run.exec("cmd /c start " + path);
                lst.setIsRunning(true);
                boolean updateresult = lstd.update(lst);
                if (updateresult == true) {
                    check = true;
                } else {
                    check = false;
                }
            } else {
                check = false;
                System.out.println("Schedule Task is Disabled Please Enable it First.");
            }
            System.out.println(tasktxt);
            System.out.println("path is///////////" + path);
            DeleteThread dt = new DeleteThread(path);
        } catch (Exception ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the following exception while trying to start the schedule task " + ex.getMessage());
        }
        return check;
    }

    //to enable Live Server Task
    public boolean enableLiveServerTask(String ip) {
        boolean check = false;
        LiveServersDao lsd = new LiveServersDao(c);
        LiveServers ls = lsd.getByIp(ip);
        LiveServersTaskDao lstd = new LiveServersTaskDao(c);
        MyLST lst = null;
        try {
            lst = lstd.getById(ls.getLsId());
            lst.setIsEnable(true);
            boolean enableresult = lstd.update(lst);
            if (enableresult == true) {
                check = true;
            } else {
                check = false;
            }
            String path = tempelement.getTextContent() + ip + "enable.bat";
            String tasktxt = "schtasks /change /tn " + ip + "ST  /enable";
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            DeleteThread dt = new DeleteThread(path);
        } catch (IOException ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the Following exception during enabling the the schedule task " + ex.getMessage());
        } catch (Exception ex) {
            LogFile.log(" System ", " encountered the Following exception during enabling the the schedule task " + ex.getMessage());
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
        return check;
    }

    //method to disable Live Server Task
    public boolean disableLiveServerTask(String ip) {
        boolean check = false;
        LiveServersDao lsd = new LiveServersDao(c);
        LiveServers ls = lsd.getByIp(ip);
        LiveServersTaskDao lstd = new LiveServersTaskDao(c);
        MyLST lst = null;
        try {
            lst = lstd.getById(ls.getLsId());
            lst.setIsEnable(false);
            lst.setIsRunning(false);
            boolean enableresult = lstd.update(lst);
            if (enableresult == true) {
                check = true;
            } else {
                check = false;
            }
            String path = tempelement.getTextContent() + ip + "disable.bat";
            String tasktxt = "schtasks /change /tn " + ip + "ST  /disable";
            File batchfile = new File(path);
            PrintWriter fos = new PrintWriter(batchfile);
            fos.println(tasktxt);
            fos.println("exit");
            fos.close();
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /c start " + path);
            System.out.println(tasktxt);
            DeleteThread dt = new DeleteThread(path);
        } catch (IOException ex) {
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
            LogFile.log(" System ", " encountered the Following exception during disabling the the schedule task " + ex.getMessage());
        } catch (Exception ex) {
            LogFile.log(" System ", " encountered the Following exception during disabling the the schedule task " + ex.getMessage());
            Logger.getLogger(ScheduledTaskWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
        return check;
    }
}
